{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "92b499a3",
   "metadata": {},
   "source": [
    "# Pandas DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5091239c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div id=2a941988-fdb4-41c8-809b-5f464674177b style=\"display:none; background-color:#9D6CFF; color:white; width:200px; height:30px; padding-left:5px; border-radius:4px; flex-direction:row; justify-content:space-around; align-items:center;\" onmouseover=\"this.style.backgroundColor='#BA9BF8'\" onmouseout=\"this.style.backgroundColor='#9D6CFF'\" onclick=\"window.commands?.execute('create-mitosheet-from-dataframe-output');\">See Full Dataframe in Mito</div> <script> if (window.commands.hasCommand('create-mitosheet-from-dataframe-output')) document.getElementById('2a941988-fdb4-41c8-809b-5f464674177b').style.display = 'flex' </script> <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>BirthDate</th>\n",
       "      <th>MathsScore</th>\n",
       "      <th>PhysicsScore</th>\n",
       "      <th>ChemistryScore</th>\n",
       "      <th>Grade</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alice</td>\n",
       "      <td>2000-01-30</td>\n",
       "      <td>90.0</td>\n",
       "      <td>87</td>\n",
       "      <td>92.0</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Bob</td>\n",
       "      <td>2001-02-03</td>\n",
       "      <td>85.0</td>\n",
       "      <td>92</td>\n",
       "      <td>NaN</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>2001-04-05</td>\n",
       "      <td>NaN</td>\n",
       "      <td>89</td>\n",
       "      <td>90.0</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table></div>"
      ],
      "text/plain": [
       "      Name  BirthDate  MathsScore  PhysicsScore  ChemistryScore Grade\n",
       "0    Alice 2000-01-30        90.0            87            92.0     A\n",
       "1      Bob 2001-02-03        85.0            92             NaN     B\n",
       "2  Charlie 2001-04-05         NaN            89            90.0     A"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data = {\n",
    "    'Name': ['Alice', 'Bob', 'Charlie'],\n",
    "    'BirthDate': ['2000-01-30', '2001-02-03', '2001-04-05'],\n",
    "    'MathsScore': [90, 85, None],\n",
    "    'PhysicsScore': [87, 92, 89],\n",
    "    'ChemistryScore': [92, None, 90],\n",
    "    'Grade' : ['A', 'B', 'A']\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data)\n",
    "df['BirthDate'] = pd.to_datetime(df['BirthDate'], format='%Y-%m-%d')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ae5df0b",
   "metadata": {},
   "source": [
    "## melt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "0722f662",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div id=77541362-979f-4ae9-ae1e-8dcd17abe635 style=\"display:none; background-color:#9D6CFF; color:white; width:200px; height:30px; padding-left:5px; border-radius:4px; flex-direction:row; justify-content:space-around; align-items:center;\" onmouseover=\"this.style.backgroundColor='#BA9BF8'\" onmouseout=\"this.style.backgroundColor='#9D6CFF'\" onclick=\"window.commands?.execute('create-mitosheet-from-dataframe-output');\">See Full Dataframe in Mito</div> <script> if (window.commands.hasCommand('create-mitosheet-from-dataframe-output')) document.getElementById('77541362-979f-4ae9-ae1e-8dcd17abe635').style.display = 'flex' </script> <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Subject</th>\n",
       "      <th>Score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Alice</td>\n",
       "      <td>MathsScore</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Bob</td>\n",
       "      <td>MathsScore</td>\n",
       "      <td>85.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>MathsScore</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Alice</td>\n",
       "      <td>PhysicsScore</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Bob</td>\n",
       "      <td>PhysicsScore</td>\n",
       "      <td>92.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>PhysicsScore</td>\n",
       "      <td>89.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Alice</td>\n",
       "      <td>ChemistryScore</td>\n",
       "      <td>92.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Bob</td>\n",
       "      <td>ChemistryScore</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Charlie</td>\n",
       "      <td>ChemistryScore</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table></div>"
      ],
      "text/plain": [
       "      Name         Subject  Score\n",
       "0    Alice      MathsScore   90.0\n",
       "1      Bob      MathsScore   85.0\n",
       "2  Charlie      MathsScore    NaN\n",
       "3    Alice    PhysicsScore   87.0\n",
       "4      Bob    PhysicsScore   92.0\n",
       "5  Charlie    PhysicsScore   89.0\n",
       "6    Alice  ChemistryScore   92.0\n",
       "7      Bob  ChemistryScore    NaN\n",
       "8  Charlie  ChemistryScore   90.0"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_melted = df.melt(id_vars='Name', \n",
    "        var_name='Subject', \n",
    "        value_name='Score', \n",
    "        value_vars=['MathsScore', 'PhysicsScore', 'ChemistryScore']\n",
    ")\n",
    "\n",
    "df_melted"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "455c88c3",
   "metadata": {},
   "source": [
    "## isnull"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "4be904fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div id=04d5fba8-5f22-4b50-a976-f2d51f4b62a3 style=\"display:none; background-color:#9D6CFF; color:white; width:200px; height:30px; padding-left:5px; border-radius:4px; flex-direction:row; justify-content:space-around; align-items:center;\" onmouseover=\"this.style.backgroundColor='#BA9BF8'\" onmouseout=\"this.style.backgroundColor='#9D6CFF'\" onclick=\"window.commands?.execute('create-mitosheet-from-dataframe-output');\">See Full Dataframe in Mito</div> <script> if (window.commands.hasCommand('create-mitosheet-from-dataframe-output')) document.getElementById('04d5fba8-5f22-4b50-a976-f2d51f4b62a3').style.display = 'flex' </script> <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>BirthDate</th>\n",
       "      <th>MathsScore</th>\n",
       "      <th>PhysicsScore</th>\n",
       "      <th>ChemistryScore</th>\n",
       "      <th>Grade</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table></div>"
      ],
      "text/plain": [
       "    Name  BirthDate  MathsScore  PhysicsScore  ChemistryScore  Grade\n",
       "0  False      False       False         False           False  False\n",
       "1  False      False       False         False            True  False\n",
       "2  False      False        True         False           False  False"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_isnull = df.isnull()\n",
    "df_isnull"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "afe185c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1     True\n",
       "2    False\n",
       "Name: ChemistryScore, dtype: bool"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['ChemistryScore'].isnull()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bf8278c",
   "metadata": {},
   "source": [
    "## dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "df4ea5cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2000\n",
       "1    2001\n",
       "2    2001\n",
       "Name: BirthDate, dtype: int64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "year = df['BirthDate'].dt.year\n",
    "year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "0dbce14a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    4\n",
       "Name: BirthDate, dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "month = df['BirthDate'].dt.month\n",
    "month"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bcedc084",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    30\n",
       "1     3\n",
       "2     5\n",
       "Name: BirthDate, dtype: int64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day = df['BirthDate'].dt.day\n",
    "day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2f9cb0de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     4\n",
       "1     5\n",
       "2    14\n",
       "Name: week, dtype: UInt32"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weekOfYear = df['BirthDate'].dt.isocalendar().week\n",
    "weekOfYear"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6321095f",
   "metadata": {},
   "source": [
    "## groupby"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "294ab3fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div id=3e3afc38-a46a-4f8f-be0f-7b51a1fe747b style=\"display:none; background-color:#9D6CFF; color:white; width:200px; height:30px; padding-left:5px; border-radius:4px; flex-direction:row; justify-content:space-around; align-items:center;\" onmouseover=\"this.style.backgroundColor='#BA9BF8'\" onmouseout=\"this.style.backgroundColor='#9D6CFF'\" onclick=\"window.commands?.execute('create-mitosheet-from-dataframe-output');\">See Full Dataframe in Mito</div> <script> if (window.commands.hasCommand('create-mitosheet-from-dataframe-output')) document.getElementById('3e3afc38-a46a-4f8f-be0f-7b51a1fe747b').style.display = 'flex' </script> <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Grade</th>\n",
       "      <th>MathsScore</th>\n",
       "      <th>PhysicsScore</th>\n",
       "      <th>ChemistryScore</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>90.0</td>\n",
       "      <td>88.0</td>\n",
       "      <td>91.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>85.0</td>\n",
       "      <td>92.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table></div>"
      ],
      "text/plain": [
       "  Grade  MathsScore  PhysicsScore  ChemistryScore\n",
       "0     A        90.0          88.0            91.0\n",
       "1     B        85.0          92.0             NaN"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_grouped = df.groupby(by='Grade').mean().reset_index()\n",
    "df_grouped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b5c53f6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
